define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/index' + location.search,
                    add_url: 'goods/add',
                    edit_url: 'goods/edit',
                    del_url: 'goods/del',
                    multi_url: 'goods/multi',
                    import_url: 'goods/import',
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'caretory', title: __('类别'), operate: 'LIKE'},
                        {field: 'breed', title: __('品种'), operate: 'LIKE'},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'describe', title: __('Describe'), operate: 'LIKE'},
                        {field: 'selltime', title: __('Selltime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'real_name', title: __('Real_name'), operate: 'LIKE'},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'manner_packing', title: __('包装方式'), operate: 'LIKE'},
                        {field: 'place_origin', title: __('产地'), operate: 'LIKE'},
                        {field: 'num', title: __('数量(公斤)'), operate: 'LIKE'},
                        {field: 'delivery_method', title: __('提货方式'), operate: 'LIKE'},
                        {field: 'is_seller', title: __('买卖方式'), searchList: {"1":__('卖出'),"2":__('买入')}, formatter: Table.api.formatter.status},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        // {field: 'exposure_fee', title: __('Exposure_fee'), operate:'BETWEEN'},
                        // {field: 'referral_fee', title: __('Referral_fee'), operate:'BETWEEN'},
                        // {field: 'premium_fee', title: __('Premium_fee'), operate:'BETWEEN'},
                        {field: 'earnest_money', title: __('Earnest_money'), operate:'BETWEEN'},
                        {field: 'is_explosive', title: __('Is_explosive'), searchList: {"1":__('Is_explosive 1'),"2":__('Is_explosive 2')}, formatter: Table.api.formatter.normal},
                        {field: 'is_overbalance', title: __('Is_overbalance'), searchList: {"1":__('Is_overbalance 1'),"2":__('Is_overbalance 2')}, formatter: Table.api.formatter.normal},
                        {field: 'is_recommend', title: __('Is_recommend'), searchList: {"1":__('Is_recommend 1'),"2":__('Is_recommend 2')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('发布时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [{
                                name: 'up',
                                title: '上架',
                                text: '上架',
                                classname: 'btn btn-xs btn-info btn-ajax',
                                icon: 'fa fa-list',
                                url: 'goods/up',
                                confirm: '是否同意该请求？', //点击按钮后的确认框，支持function
                                visible: function (row) {
                                    // 判断当前行数据中的 status 字段的值是否等于 1
                                    if (row.status == 2) {
                                        // return true 代表显示
                                        return true;
                                    }
                                },
                                success: function (data, ret) {
                                    $(".btn-refresh").trigger("click");
                                    //如果需要阻止成功提示，则必须使用return false;
                                    return true;
                                },
                                //callback: function (data) {
                                //    Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                //}
                            },{
                                name: 'down',
                                title: '下架',
                                text: '下架',
                                classname: 'btn btn-xs btn-primary btn-ajax',
                                icon: 'fa fa-list',
                                url: 'goods/down',
                                confirm: '是否同意该请求？', //点击按钮后的确认框，支持function
                                visible: function (row) {
                                    // 判断当前行数据中的 status 字段的值是否等于 1
                                    if (row.status == 1) {
                                        // return true 代表显示
                                        return true;
                                    }
                                },
                                success: function (data, ret) {
                                    $(".btn-refresh").trigger("click");
                                    //如果需要阻止成功提示，则必须使用return false;
                                    return true;
                                },
                                //callback: function (data) {
                                //    Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                //}
                            }]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'goods/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'goods/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'goods/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        up: function () {
            Controller.api.bindevent();
        },
        down: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
